package leetcode;

//给定一个二叉树，检查它是否是镜像对称的。
//例如，二叉树 [1,2,2,3,4,4,3] 是对称的。

public class problems_101 {

    public static void main(String[] arge) {
        TreeNode A1 = new TreeNode(1);
        TreeNode A2 = new TreeNode(2);
        TreeNode A3 = new TreeNode(2);
        TreeNode A4 = new TreeNode(3);
        TreeNode A5 = new TreeNode(4);
        TreeNode A6 = new TreeNode(4);
        TreeNode A7 = new TreeNode(3);
        A1.left = A2;
        A1.right = A3;
        A2.left = A4;
        A2.right = A5;
        A3.left = A6;
        A3.right = A7;
        boolean ret = new Solution().isSymmetric(A1);
        System.out.println(ret);
    }
    static class Solution {
        public boolean isSymmetric(TreeNode root) {
            if(null == root) return true;
            return checkNode(root.left, root.right);
        }
        private boolean checkNode(TreeNode p, TreeNode q){
            if(null == p && null == q) return true;
            if(null == p || null == q) return false;
            if(p.val != q.val) return false;
            return checkNode(p.left, q.right) && checkNode(p.right, q.left);
        }
    }
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
}
